'use strict';
const {
  Model
} = require('sequelize');
const { Op } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Brand extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      models.Brand.hasMany(models.Series, {as: 'series'})
    }
  }
  Brand.init({
    nameZh: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notNull: {msg: '中文品牌名称必填'},
        notEmpty: {msg: '中文品牌名称不能为空'},
        len: {
          args: [2, 45],
          msg: '中文品牌名称长度在2-45个字符之间'
        },
        async isUnique(value) {
          const brand = await Brand.findOne({ where: { nameZh: value } })
          if (brand) {
            throw new Error('中文品牌名称已经存在。');
          }
        }
      }
    },
    nameEn: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notNull: {msg: '英文品牌名称必填'},
        notEmpty: {msg: '英文品牌名称不能为空'},
        len: {
          args: [2, 45],
          msg: '英文品牌名称长度在2-45个字符之间'
        },
        async isUnique(value) {
          const brand = await Brand.findOne({ where: { nameEn: value } })
          if (brand) {
            throw new Error('英文品牌名称已经存在。');
          }
        }
      }
    },
    img: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notNull: {msg: '品牌图片必填'},
        notEmpty: {msg: '品牌图片不能为空'},
        // isUrl: {msg: '品牌图片必须是一个有效的URL'}
      }
    },
    descriptionZh: DataTypes.TEXT,
    descriptionEn: DataTypes.TEXT
  }, {
    sequelize,
    modelName: 'Brand',
  });
  return Brand;
};